package com.zyh.supermark.system.service.impl;

import com.zyh.supermark.system.controller.UserController;
import com.zyh.supermark.system.dao.impl.UserDaoImpl;
import com.zyh.supermark.system.domain.User;
import com.zyh.supermark.system.service.UserService;

import java.util.ArrayList;
import java.util.Scanner;

public class UserServiceImpl implements UserService {
    public static final ArrayList<User> USER_LIST = UserDaoImpl.USER_LIST;

    public static final Scanner INPUT = UserController.INPUT;

    public static final UserDaoImpl daoImpl = new UserDaoImpl();

    //查询所有
    @Override
    public void findAll() {
        daoImpl.findAll();
    }

    //删除
    @Override
    public boolean delete(int id) {
        int deleteId = daoImpl.findById(id);
        if (deleteId == -1) {
            System.out.println("您要删除的信息不存在");
            return false;
        }
        System.out.println("您要删除的信息如下：");
        for (int i = 0; i < USER_LIST.size(); i++) {
            User user = USER_LIST.get(i);
            if (deleteId == i) {
                System.out.println(user.getUserId() + "\t" + user.getUserName() + "\t" + user.getCardType() + "\t" + user.getScore() + "\t" + user.getCardState());
            }
        }
        System.out.println("确认是否删除y/n");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            daoImpl.delete(id);
            System.out.println("删除成功");
            daoImpl.findAll();
        }
        return true;
    }

    //添加信息
    @Override
    public boolean add(User user) {
        daoImpl.add(user);
        System.out.println("添加成功");
        return true;
    }

    @Override
    public int findById(int id) {
        int byId = daoImpl.findById(id);
        return byId;
    }
}
